﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAO
{
    public class PurchaseDao
    {
        public PurchaseDao()
        {
            SqlHelper sql = new SqlHelper();
        }
        
        public string insertPurchase(int id,
            string name, string notes,
            int month, float totalAmoun,
            string image, DateTime purchaseDate)
        {
            
            return SqlHelper.getString("SAVE_PURCHASE",
                "@ID", id,
                "@NAME", name,
                "@NOTE", notes,
                "@IMAGE", image,
                "@TOTAL_MONTH", month,
                "@TOTAL_ANOUN", totalAmoun,
                "@PURCHASE_DATE", purchaseDate);
        }

        public string insertPurchaseDayOnly(int id,
            string name, string notes,
            int month, float totalAmoun,
            string image, DateTime purchaseDate)
        {

            return SqlHelper.getString("SAVE_PURCHASE_DAY_ONLY",
                "@ID", id,
                "@NAME", name,
                "@NOTE", notes,
                "@IMAGE", image,
                "@TOTAL_MONTH", month,
                "@TOTAL_ANOUN", totalAmoun,
                "@PURCHASE_DATE", purchaseDate);
        }

        public string updateCustomer(int id, string name, string notes, string image)
        {
            return SqlHelper.getString("UPDATE_CUSTOMER",
               "@ID", id,
               "@NAME", name,
               "@NOTE", notes,
               "@IMAGE", image);
        }

        
        public DataTable getCustommerWarning(string name)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMERS_WARNING", CommandType.StoredProcedure,
                "@NAME", name);
        }

        public DataTable getCustommer(string name, string active)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMERS", CommandType.StoredProcedure,
                "@NAME", name,
                "@ACTIVE", active);
        }

        public DataTable getCustommerInMonth(int month)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMER_IN_MONTH", CommandType.StoredProcedure,
                "@MONTH", month);
        }


        public DataTable getCustommerDuringTime(DateTime fromDate, DateTime toDate)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMER_DURING_TIME", CommandType.StoredProcedure,
                "@FROM_DATE", fromDate,
                "@TO_DATE", toDate);
        }

        public DataTable getCustommerAw(int month)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMER_AW", CommandType.StoredProcedure,
                "@MONTH", month);
        }


        public DataTable getCustommerFifo(string name, string active)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMERS_FIFO", CommandType.StoredProcedure,
                "@NAME", name,
                "@ACTIVE", active);
        }

        public DataTable getCustommerPurchaseHist(int id)
        {
            return SqlHelper.ExecuteDataTable("GET_PURCHASE_HIST", CommandType.StoredProcedure,
                "@ID", id);
        }

        public bool deleteCustomer(int id)
        {
            return SqlHelper.ExecuteNonQuery("DELETE_CUSTOMER", CommandType.StoredProcedure,
                "@ID", id);
        }

        public bool remindOrUnremindCustomer(int id)
        {
            return SqlHelper.ExecuteNonQuery("SP_REMIND_UN_REMIND_CUSTOMER", CommandType.StoredProcedure,
                "@ID", id,
                "@USR_ID", LoginDao.userID);
        }
        

        public DataTable GetCustomerPurchase(DateTime fromDate, DateTime toDate)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMER_PURCHASE", CommandType.StoredProcedure,
                "@FORM_DATE", fromDate,
                "@TO_DATE", toDate);
        }

        public DataTable GetCustomerDayOnly(string name)
        {
            return SqlHelper.ExecuteDataTable("GET_CUSTOMERS_DAY_ONLY", CommandType.StoredProcedure,
                "@NAME", name);
        }



        public DataRow getCustomer(int id)
        {
            DataTable table  = SqlHelper.ExecuteDataTable("GET_CUSTOMER", CommandType.StoredProcedure, "@ID", id);
            if (table != null && table.Rows.Count > 0)
            {
                return table.Rows[0];
            }
            return null;
        }

        public DateTime? getMaxDateRemind(int id)
        {
            DataTable table = SqlHelper.ExecuteDataTable("GET_MAX_DATE_REMIND", CommandType.StoredProcedure, "@ID", id);
            if (table != null && table.Rows.Count > 0)
            {
                DateTime Mmax;
                if (DateTime.TryParse(table.Rows[0][0].ToString(), out Mmax))
                {
                    return Mmax;
                }
                else
                {
                    return null;
                }
            }
            return null;
        }

        public DataRow getCustomerHist(int id)
        {
            DataTable table = SqlHelper.ExecuteDataTable("GET_CUSTOMER_HIST", CommandType.StoredProcedure, "@ID", id);
            if (table != null && table.Rows.Count > 0)
            {
                return table.Rows[0];
            }
            return null;
        }


        public DataRow getReport(DateTime fromDate, DateTime toDate)
        {
            DataTable table = SqlHelper.ExecuteDataTable("GET_REPORT", CommandType.StoredProcedure,
                "@FROM_DATE", fromDate,
                "@TO_DATE", toDate);
            if (table != null && table.Rows.Count > 0)
            {
                return table.Rows[0];
            }
            return null;
        }

        public DataRow getPurchase(int purchaseId)
        {
               DataTable table = SqlHelper.ExecuteDataTable("GET_PURCHASE_FOR_CANCEL", CommandType.StoredProcedure,
                "@ID", purchaseId);
            if (table != null && table.Rows.Count > 0)
            {
                return table.Rows[0];
            }
            return null;
        }

        public bool cancelPurchase(int purchaseId, string notes)
        {
            return SqlHelper.ExecuteNonQuery("DO_CANCEL_PURCHASE", CommandType.StoredProcedure,
                "@PURCHASE_ID", purchaseId,
                "@NOTES", notes,
                "@USR_ID", LoginDao.userID);
        }
    }
}
